package dfsketches.gas;

public class PrimitiveGasProcessor implements GasProcessor {

	private GasMap gm;
	
	public PrimitiveGasProcessor(GasMap map) {
		gm = map;
	}
	
	@Override
	public void process() {
		int w = gm.getWidth();
		int h = gm.getHeight();
		int d = gm.getDepth();
		
		for (Gas g : gm.getConcentrations()) {
			boolean n1 = (g.x > 0) && (gm.isGasPassable(g.x-1, g.y, g.z));
			boolean n2 = (g.x < w-1) && (gm.isGasPassable(g.x+1, g.y, g.z));
			boolean n3 = (g.y > 0) && (gm.isGasPassable(g.x, g.y-1, g.z));
			boolean n4 = (g.y < h-1) && (gm.isGasPassable(g.x, g.y+1, g.z));
			boolean n5 = (g.z > 0) && (gm.isGasPassable(g.x, g.y, g.z-1));
			boolean n6 = (g.z < d-1) && (gm.isGasPassable(g.x, g.y, g.z+1));
			
			int nn = 1;
			if (n1) nn++;
			if (n2) nn++;
			if (n3) nn++;
			if (n4) nn++;
			if (n5) nn++;
			if (n6) nn++;
			int average = g.c / nn;

			gm.setConcentration(g.mat, g.x, g.y, g.z, average);
			if (average > 0) {
				if (n1) gm.increaseConcentration(g.mat, g.x-1, g.y, g.z, average);
				if (n2) gm.increaseConcentration(g.mat, g.x+1, g.y, g.z, average);
				if (n3) gm.increaseConcentration(g.mat, g.x, g.y-1, g.z, average);
				if (n4) gm.increaseConcentration(g.mat, g.x, g.y+1, g.z, average);
				if (n5) gm.increaseConcentration(g.mat, g.x, g.y, g.z-1, average);
				if (n6) gm.increaseConcentration(g.mat, g.x, g.y, g.z+1, average);
			}
		}
	}

}